<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>  
<!DOCTYPE html>
<html>
<head>
<title>新增用户</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<jsp:include page="/WEB-INF/views/include/newhead.jsp" />
<%@include file="/WEB-INF/views/include/treeview.jsp"%>

</head>
<body>

<body class="childrenBody site-block">
	<header class="larry-personal-tit">
		<span><c:if test="${empty user.id}">新增</c:if><c:if test="${not empty user.id}">修改</c:if>
		</span>
	</header><!-- /header -->
	
	<div class="mainCon">
      <form:form id="inputForm" modelAttribute="user" action="${ctx}/sys/user/save" method="post" >
		<form:hidden path="id" id="userId"/>
		<sys:message content="${message}"/>
		
		 <div class="layui-form-item">
			<label class="layui-form-label"><font color="red">*</font>所属公司</label>
			<div class="layui-input-inline layui-input-inline-one">
				<sys:treeselect id="company" name="company.id" value="${user.company.id}" 
	 				labelName="company.name" labelValue="${user.company.name}"
					title="公司" url="/sys/office/treeData?type=1&isReadCache=false"  
					completeJs="filterOffice();" cssClass="layui-input layui-input-treeselect required"/>
			</div>
		</div>
		
		<div class="layui-form-item">
			<label class="layui-form-label"><font color="red">*</font>所属部门</label>
			<div class="layui-input-inline layui-input-inline-one">
			  <sys:treeselect id="office" name="office.id" value="${user.office.id}" 
				labelName="office.name" labelValue="${user.office.name}" disabled="disabled"
				title="部门" url="/sys/office/treeData?isReadCache=false" cssClass="layui-input layui-input-treeselect required" 
				completeJs="filterRole();" notAllowSelectParent="true"/>
			</div>
		</div>
		
		<div class="layui-form-item">
			<label class="layui-form-label"><font color="red">*</font>用户角色</label>
			<div class="layui-input-inline layui-input-inline-one">
		      <sys:dialogTableSelect id="role" name="roleIds" value="${user.roleIds}"
				cssClass="layui-input layui-input-treeselect required" labelName="" labelValue="${user.roleNames}" title="岗位列表"
				width="1000" tableSelectId="id" tableSelectName="name" height="600" allowClear="true"
				includeUrl="/WEB-INF/views/modules/sys/roleListBydialog.jsp?roleType=1"
				singleSelect="false" params="" disabled="disabled" draggable="true"/>
			</div>
		</div>
		
		<div class="layui-form-item">
		   <div class="layui-inline">
			<label class="layui-form-label"><font color="red">*</font>姓名</label>
			<div class="layui-input-inline">
				<input class="layui-input required" name="name" value="${user.name}"  placeholder="请输入姓名" type="text">
			</div>
		   </div>
		   
		   <div class="layui-inline">
			<label class="layui-form-label"><font color="red">*</font>登录名</label>
			<div class="layui-input-inline">
			    <input id="oldLoginName" name="oldLoginName" type="hidden" value="${user.loginName}">
				<form:input id="loginName" path="loginName"  placeholder="请输入登录名" class="layui-input required userName"/>
			</div>
		    </div>
		</div>
	
		<div class="layui-form-item">
		 <div class="layui-inline">
			<label class="layui-form-label">
			  <c:if test="${empty user.id}"><font color="red">*</font></c:if>
			  密码</label>
			<div class="layui-input-inline">
			    <c:if test="${empty user.id}">
			      <input id="newPassword" name="newPassword" type="password" placeholder="请输入密码" value=""  minlength="6" class="layui-input ${empty user.id?'required':''}"/>
				</c:if>	
			    <c:if test="${not empty user.id}">
			      <input id="newPassword" name="newPassword" type="password" placeholder="若不修改密码，请留空" value=""  minlength="6" class="layui-input ${empty user.id?'required':''}"/>
				</c:if>	
			</div>
		  </div>
		  
		  <div class="layui-inline">
			<label class="layui-form-label">
			 <c:if test="${empty user.id}"><font color="red">*</font></c:if>
			 确认密码</label>
			<div class="layui-input-inline">
			     <input id="confirmNewPassword" name="confirmNewPassword" placeholder="请确认密码" type="password" value="" maxlength="50" minlength="3" equalTo="#newPassword" class="layui-input"/>
			</div>
			</div>
	   </div>

		<div class="layui-form-item">
		   <div class="layui-inline">
			<label class="layui-form-label">人员编号</label>
			<div class="layui-input-inline">
			    <input type="hidden" id="noOld" name="noOld" value="${user.no}" />
			    <form:input id="no" path="no"  placeholder="请输入人员编号" htmlEscape="false" maxlength="50" class="layui-input"/>
			</div>
		    </div>
		    
		   <div class="layui-inline">
			<label class="layui-form-label">是否允许登录</label>
			<div class="layui-input-inline">
				<form:select path="loginFlag" cssClass="select2_" >
					<form:options items="${fns:getDictList('yes_no')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
				</form:select>    
			</div>
		    </div>
		</div>
		<div class="layui-form-item">
		  <div class="layui-inline">
			<label class="layui-form-label">用户类型</label>
			<div class="layui-input-inline">
				<form:select path="userType" cssClass="select2_" >
					<form:option value="" label="请选择"/>
					<form:options items="${fns:getDictList('sys_user_type')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
				</form:select>    
			</div>
		 </div>
		 
		 <div class="layui-inline">
			<label class="layui-form-label">电话</label>
			<div class="layui-input-inline">
			    <form:input path="phone"  placeholder="请输入联系电话" htmlEscape="false" maxlength="100" class="layui-input"/>
			</div>
		  </div>
	</div>
	
		<div class="layui-form-item">
		   <div class="layui-inline">
			<label class="layui-form-label">手机</label>
			<div class="layui-input-inline">
			    <form:input path="mobile" placeholder="请输入联系手机" htmlEscape="false" maxlength="100" class="layui-input mobile"/>
			</div>
		   </div>
		   
		   <div class="layui-inline">
			<label class="layui-form-label">Email</label>
			<div class="layui-input-inline">
			    <form:input path="email" placeholder="请输入Email" htmlEscape="false" maxlength="100" class="layui-input email"/>
			</div>
		   </div>
		</div>
	<%-- <div class="layui-form-item">
			<label class="layui-form-label">传真</label>
			<div class="layui-input-inline input500">
				<input class="layui-input" placeholder="请输入传真" type="text"  >
			</div>
		</div> --%>	

       <div class="layui-form-item">
           <div class="layui-inline">
			<label class="layui-form-label">职务</label>
			<div class="layui-input-inline">
			    <form:input path="positionName" placeholder="请输入职务" htmlEscape="false" maxlength="100" class="layui-input"/>
			</div>
		    </div>
		</div>  
		
        <div class="layui-form-item">
			<label class="layui-form-label">备注</label>
			<div class="layui-input-inline layui-input-inline-one">
			    <form:textarea path="remarks" htmlEscape="false" rows="4" maxlength="200"  class="layui-textarea"/>
			</div>
		</div>
		<div class="layui-form-item bottom_xf">
			<div class="layui-input-block input500">
			    <button type="button" class="layui-btn layui-btn-primary" onclick="gotoView('${ctx}/sys/user/list')">返回</button>
				<button class="layui-btn" type="submit">保存</button>
				<button onclick="formReset();" type="button"  class="layui-btn layui-btn-danger">重置</button>
		    </div>
		</div>
	</form:form>
    </div>

	<script type="text/javascript">
		$(document).ready(function() {
			$("#inputForm").validate({
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
			   rules: {
				        loginName: {          //id或者name属性的名称
			                 remote: {
			                     type: "post",  //请求方式
			                     url: "${ctx}/sys/user/checkLoginName",        //发送请求的url地址
			                     data: {
			                    	    loginName: function() {
			                                return $("#loginName").val();      
			                             },
			                             oldLoginName: function() {
			                                return $("#oldLoginName").val();      
			                             }
			                     },
			                    dataType: "text",        //发送的数据类型
			                    dataFilter: function(data, type) { //返回结果
			                        if (data == "true")
			                             return true;
			                         else
			                             return false;
			                     }
			                 }
			             },
			             no: {          //id或者name属性的名称
				                 remote: {
				                     type: "post",  //请求方式
				                     url: "${ctx}/sys/user/checkNo",        //发送请求的url地址
				                     data: {
				                    	     no: function() {
				                                return $("#no").val();      
				                             },
				                             noOld: function() {
				                                return $("#noOld").val();      
				                             },
				                             id: function() {
					                                return $("#userId").val();      
					                         }
				                     },
				                    dataType: "text",        //发送的数据类型
				                    dataFilter: function(data, type) { //返回结果
				                        if (data == "true")
				                             return true;
				                         else
				                             return false;
				                     }
				                 }
				             }
			   },
				messages: {
					loginName: {remote: "登录名已经存在"},
			        no: {remote: "人员代码已经存在"}
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
			
			//doAajax();
		});
	
		
		//根据选择的公司过滤掉部门  add for lgx 20160606
		function  filterOffice(){
			var companyId= $('#companyId').val();
			var officeUrl = $('#officeUrl').val();
			if ($("#officeButton").hasClass("disabled")){
				$("#officeButton").removeClass("disabled");
			}
			//清空部门
			$('#officeId').val("");
			$('#officeName').val("");
			var array  = officeUrl.split("&");
			$('#officeUrl').val(array[0]+"&type=2&parentId="+companyId);
			
			$('#roleId').val("");
			$('#roleName').val("");
			//doAajax();
		}
		
		function filterRole(){
			var officeId= $('#officeId').val();
			var roleurl = $('#roleurl').val();
			if ($("#roleButton").hasClass("disabled")){
				$("#roleButton").removeClass("disabled");
				$("#roleButton_clear").removeClass("disabled");
			}
			//清空部门
			$('#roleId').val("");
			$('#roleName').val("");
			var array  = roleurl.split("&");
			$('#roleurl').val(array[0]+"&officeId="+officeId+"&officeIdOrRoleType=true&asd=");
		}
		
		
		//清空部门
		function initOffice(){
			//清空部门
			$('#officeId').val("");
			$('#officeName').val("");
		}
		
		var setting = {
			check:{
				enable: true,
				chkboxType: { "Y" : "s", "N" : "s" },
				nocheckInherit: true
			},
			view:{
				selectedMulti : false
			},
			data : {
				simpleData : {
					enable : true
				}
			},
			callback : {
				beforeClick : function(id, node) {
					tree.checkNode(node, !node.checked, true, true);
					return false;
				}
			}
		};

		function doAajax(){
			var companyId = $("#companyId").val();
			var  id = $("#userId").val();
			// alert(id);
			var num = "";
			for (var i = 0; i < 6; i++) {
				num += Math.floor(Math.random() * 10);
			}
			
			//异步加载角色
			$.get("${ctx}/sys/user/findRoleByCompanyAjax?companyId="+companyId+"&num="+num+"&userId="+id, function(data){
				$("#roleList_div").html(data);
		    });
			
		}
		
	</script>
	
	<jsp:include page="/WEB-INF/views/include/pageJs.jsp" />
</body>
</html>